const { app, BrowserWindow, Menu, globalShortcut } = require("electron");
const { resolve } = require("path");
const WinState = require("electron-win-state").default;
const mainMenu = require("./mainMenu");
// const getSource = require("./controller/getSource");
/** 图片裁剪 */
require("./controller/getSource");
/** alert */
require("./controller/alert");
/** open window */
require("./controller/openWindow");
/** get FileList */
require("./controller/getFileList");
/** 菜单 */
require("./controller/buildMenu");

const createTray = require("./tray");

const WIN_STATE = new WinState({
    defaultWidth: 800,
    defaultHeight: 1000,
    electronStoreOptions: {
        name: "window-state-main"
    }
});


const CREATE_WINDOW = () => {
    const WIN = new BrowserWindow({
        // 自定义窗口状态
        ...WIN_STATE.winOptions,
        webPreferences: {
            // 自定义预加载的js 
            preload: resolve(__dirname, "./preload"),
            nodeIntegration: true
        },

        show: false,
        icon: resolve(__dirname, "basketball.png")
    });
    WIN.setIcon('basketball.png');
    WIN.loadURL("http://localhost:5173");
    WIN.webContents.openDevTools();// 控制台
    WIN_STATE.manage(WIN);
    WIN.on("ready-to-show", () => {
        WIN.show();
    });
    // 当你想要刷新窗口时，调用以下函数
    const refreshWindow = () => WIN.webContents.reload();

    globalShortcut.register("f5", () => {
        refreshWindow()

    });
    globalShortcut.register("f6", () => {
        WIN.webContents.openDevTools();// 控制台
    })
    // Menu.setApplicationMenu(mainMenu("就是太帅了，无法解释", (result) => {
    //     console.log(result);
    // }));

    // getSource();
    createTray(app, WIN);

};
app.whenReady().then(() => {
    CREATE_WINDOW();

    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            console.log("activate")
            CREATE_WINDOW()
        };
    })
});

app.on("window-all-closed", () => {
    if (process.platform === "darwin") app.quit();
});
